package persistence;

import java.sql.Timestamp;
import java.util.HashSet;

import model.Addition;
import model.Category;
import model.Customer;
import model.Trade;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class TestClass {

	/**
	 * @param args
	 */
	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		test_insert();
		//CustomerDao.modifyImage(4, "d:\\ctrlz.jpg");
		
	}
	static void test_insert(){
		Customer c = new Customer();
		c.setName("ta");
		c.setPassword("tapassword");
		c.setEmail("taemail");
		c.setPhone("taphone");
		c.setTrade(new HashSet());
		
		Trade t1 = new Trade();
		t1.setTitle("aaa");
		t1.setHighPrice(999);
		t1.setLowPrice(0);
		
		Trade t2 = new Trade();
		t2.setTitle("bbb");
		t2.setHighPrice(999);
		t2.setLowPrice(0);
		
		Trade t3 = new Trade();
		t3.setTitle("ccc");
		t3.setHighPrice(999);
		t3.setLowPrice(0);
		
		
		CheckAndRegister.register(c);	
		LoadAndUpdate.modifyName(c, "guali");
	}
	
	
	static void test_modify(){
		Session session=HibernateUtil.openSession();
		Transaction tx = null;
		
		try {
			
			//��ʼ����
			tx=session.beginTransaction();
			
			Customer c =new Customer();
			c.setName("tb");
			c.setPassword("tapassword");
			c.setEmail("taemail");
			c.setPhone("taphone");
			c.setTrade(new HashSet());
			
			Trade t1 = new Trade();
			t1.setTitle("aaa");
			t1.setHighPrice(999);
			t1.setLowPrice(0);
			
			Trade t2 = new Trade();
			t2.setTitle("bbb");
			t2.setHighPrice(999);
			t2.setLowPrice(0);
			
			Trade t3 = new Trade();
			t3.setTitle("ccc");
			t3.setHighPrice(999);
			t3.setLowPrice(0);
			
			t1.setOwner(c);
			t2.setOwner(c);
			t3.setOwner(c);
			
			c.getTrade().add(t3);
			c.getTrade().add(t1);
			c.getTrade().add(t2);
			

			session.save(c);
			
			//�ύ����
			tx.commit();
			
		} catch(Exception e){
			System.err.println(e.getMessage());
			
			//�ع�����
			if(tx != null){
				tx.rollback();
			}
			
		} finally {
			if (session != null && session.isOpen()){
				session.close();
			}

		}
	}
	
	
	static void test_tree(){
		Session session=HibernateUtil.openSession();
		Transaction tx = null;
		
		try {
			
			//��ʼ����
			tx=session.beginTransaction();
			
			Category food = new Category("food", null, new HashSet());
			Category fruit = new Category("fruit", null, new HashSet());
			Category vegetable = new Category("vegetable", null, new HashSet());
			Category apple = new Category("apple", null, new HashSet());
			Category orange = new Category("orange", null, new HashSet());
			Category tomato = new Category("tomato", null, new HashSet());
			
			food.getChildrenCategory().add(fruit);
			fruit.setParentCategory(food);
			
			food.getChildrenCategory().add(vegetable);
			vegetable.setParentCategory(food);
			
			fruit.getChildrenCategory().add(apple);
			apple.setParentCategory(fruit);
			
			fruit.getChildrenCategory().add(orange);
			orange.setParentCategory(fruit);
			
			vegetable.getChildrenCategory().add(tomato);
			tomato.setParentCategory(vegetable);

			session.save(food);
			
			//�ύ����
			tx.commit();
			
		} catch(Exception e){
			System.err.println(e.getMessage());
			
			//�ع�����
			if(tx != null){
				tx.rollback();
			}
			
		} finally {
			if (session != null && session.isOpen()){
				session.close();
			}

		}
		
		

	}
	
	/**
	 * 
	 * @param name
	 * @param password
	 * @param email
	 * @param title
	 * @param mode
	 * @param date
	 */
//	static void add_trade(String name, String password, String email, String title, int mode, Date date){
//			
//		Customer c =new Customer();
//		c.setName(name);
//		c.setPassword(password);
//		c.setEmail(email);
//
//		c.setTrade(new HashSet());
//		
//
//		Trade t = new Trade();
//		t.setTitle(title);
//		t.setMode(mode);
//		t.setDate(date);
//		
//		AddTrade.addTrade(c, t);
//
//		
//
	// }

}
